<template>
  <el-dialog
    :title="!order.orderId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="order" :rules="dataRule" ref="order" @keyup.enter.native="orderSubmit()" label-width="80px">
    <el-form-item label="秒杀活动中间表" prop="middleId">
      <el-input v-model="order.middleId" placeholder="秒杀活动中间表"></el-input>
    </el-form-item>
    <el-form-item label="邮费价格" prop="postage">
      <el-input v-model="order.postage" placeholder="邮费价格"></el-input>
    </el-form-item>
    <el-form-item label="订单创建时间" prop="orderCreateTime">
      <el-input v-model="order.orderCreateTime" placeholder="订单创建时间"></el-input>
    </el-form-item>
    <el-form-item label="付款时间" prop="accountTime">
      <el-input v-model="order.accountTime" placeholder="付款时间"></el-input>
    </el-form-item>
    <el-form-item label="订单状态【0:待付款 1:已付款】" prop="orderStatus">
      <el-input v-model="order.orderStatus" placeholder="订单状态【0:待付款 1:已付款】"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="orderSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        order: {
          orderId: 0,
          middleId: '',
          postage: '',
          orderCreateTime: '',
          accountTime: '',
          orderStatus: ''
        },
        dataRule: {
          middleId: [
            { required: true, message: '秒杀活动中间表不能为空', trigger: 'blur' }
          ],
          postage: [
            { required: true, message: '邮费价格不能为空', trigger: 'blur' }
          ],
          orderCreateTime: [
            { required: true, message: '订单创建时间不能为空', trigger: 'blur' }
          ],
          accountTime: [
            { required: true, message: '付款时间不能为空', trigger: 'blur' }
          ],
          orderStatus: [
            { required: true, message: '订单状态【0:待付款 1:已付款】不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.order.orderId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['order'].resetFields()
          if (this.order.orderId) {
            this.$http({
              url: this.$http.adornUrl(`/seckill/orderform/info/${this.order.orderId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.order.middleId = data.orderForm.middleId
                this.order.postage = data.orderForm.postage
                this.order.orderCreateTime = data.orderForm.orderCreateTime
                this.order.accountTime = data.orderForm.accountTime
                this.order.orderStatus = data.orderForm.orderStatus
              }
            })
          }
        })
      },
      // 表单提交
      orderSubmit () {
        this.$refs['order'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/seckill/orderform/${!this.order.orderId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'orderId': this.order.orderId || undefined,
                'middleId': this.order.middleId,
                'postage': this.order.postage,
                'orderCreateTime': this.order.orderCreateTime,
                'accountTime': this.order.accountTime,
                'orderStatus': this.order.orderStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
